﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileMonitor.Service.Prototype;
using FileMonitor.Service.ProcessMonitor.Model;

namespace FileMonitor.Service.ProcessMonitor.Manager
{
    public class ProcessOperationManager
    {
        static ProcessOperationManager instance;
        private static ProcessOperationManager lockObj = new ProcessOperationManager();
        internal static ProcessOperationManager Current
        {
            get
            {
                if (instance == null)
                {
                    lock (lockObj)
                    {
                        if (instance == null)
                        {
                            instance = new ProcessOperationManager();
                        }
                    }
                }
                return instance;
            }
        }

        private ProcessOperationManager()
        {
            
        }

        public OperationResult ReverseFile(FileInfo fi, string processName)
        {
            try
            {
                string monitorPath = ProcessManager.Processes[processName].MonitorPath;
                string fileName = fi.FileName;
                string extName = System.IO.Path.GetExtension(fileName);
                if (string.Compare(extName, fileName, true) == 0)
                {
                    fileName = fileName.TrimEnd(extName.ToCharArray());
                }
                System.IO.File.Move(fi.FullPath, System.IO.Path.Combine(monitorPath, fileName));
                
                ProcessStateMonitorManager.Current.RemoveFileInfo(fi.FileName, ProcessManager.Processes[processName]);
                return new OperationResult() { Flag = 1 };
            }
            catch (Exception ex)
            {
                return new OperationResult() { Flag = 0, Message=ex.Message};
            }
        }

        public OperationResult StartProcess(string processName)
        {
            try
            {
                ProcessManager.Processes[processName].Start();
                return new OperationResult() { Flag = 1 };
            }
            catch (Exception ex)
            {
                return new OperationResult() { Flag = 0, Message = ex.Message };
            }
        }

        public OperationResult StopProcess(string processName)
        {
            try
            {
                ProcessManager.Processes[processName].Stop();
                return new OperationResult() { Flag = 1 };
            }
            catch (Exception ex)
            {
                return new OperationResult() { Flag = 0, Message = ex.Message };
            }

        }

        public OperationResult RefreshProcesses()
        {
            try
            {
                FileProcessProviderCollection currentCollection = ProcessManager.Processes;
                FileProcessProviderCollection updatedCollection = Provider.ProcessProvider.ProcessProvider.GetAllProcess();

                foreach (FileMonitorBase process in updatedCollection)
                {
                    FileMonitorBase currentProcess = currentCollection[process.Name];
                    if (currentProcess == null)
                    {
                        ProcessManager.Processes.Add(process);
                    }
                }
                foreach (FileMonitorBase process in currentCollection)
                {
                    FileMonitorBase updateProcess = updatedCollection[process.Name];
                    if (updateProcess == null)
                    {
                        ProcessManager.Processes.Remove(process.Name);
                    }
                }
                return new OperationResult() { Flag = 1 };
            }
            catch (Exception ex)
            {
                return new OperationResult() { Flag = 0, Message = ex.Message };
            }
        }

        public Dictionary<string, MonitorProcessInfo> GetMonitorProcessInfo()
        {
            Dictionary<string, MonitorProcessInfo> mis = new Dictionary<string, MonitorProcessInfo>();
            foreach (FileMonitorBase process in ProcessManager.Processes)
            {
                MonitorProcessInfo mi = new MonitorProcessInfo(process);
                mi.ProcessedFileInfos = Manager.ProcessStateMonitorManager.Current.GetFileInfos(process,FileState.AllInProcessBinding);
                mis.Add(process.Name, mi);
            }
            return mis;
        }
    }
}
